package com.banshuibao.bsb_android.http;

import android.text.TextUtils;

import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.model.CalenderFormModel;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zyh on 2017/2/13.
 */

public class OkHttp2366PostRequest implements ObservableOnSubscribe<String> {

    private String url;

    private CalenderFormModel.Body params;

    public OkHttp2366PostRequest(String url, CalenderFormModel.Body params) {
        this.url = url;
        this.params = params;
    }

    private RequestBody createBody() {
        return new FormBody.Builder().add("ny", params.getNy()).add("r",
                String.valueOf(params.getR())).build();
    }

    @Override
    public void subscribe(ObservableEmitter<String> e) throws Exception {
        Request request = new Request.Builder().
                url(url).
                post(createBody()).
                build();
        Response response = OkHttpClientUtil.getInstance().getClient().newCall(request).execute();
        if (response.code() == 200)
            e.onNext(response.body().string());
        else
            e.onError(new Throwable("NET_ERROR"));
        e.onComplete();
    }
}
